/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xdtech.coder.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.coder.dao.ModelTableMappedDao;
import com.xdtech.coder.model.ModelTable;
import com.xdtech.coder.model.ModelTableMapped;
import com.xdtech.coder.service.ModelTableMappedService;
import com.xdtech.coder.service.ModelTableService;
import com.xdtech.coder.vo.ModelTableMappedItem;
import com.xdtech.core.model.BaseItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2016-3-6下午7:52:59
 * @since 1.0
 * @see
 */
@Service
public class ModelTableMappedServiceImpl implements ModelTableMappedService{

	@Autowired
	private ModelTableMappedDao modelTableMappedDao;
	@Autowired
	private ModelTableService modelTableService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param entity
	 */
	@Override
	public void save(ModelTableMapped entity) {
		modelTableMappedDao.save(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param entities
	 */
	@Override
	public void saveAll(List<ModelTableMapped> entities) {
		modelTableMappedDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param entity
	 */
	@Override
	public void delete(ModelTableMapped entity) {
		modelTableMappedDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param id
	 */
	@Override
	public void delete(Long id) {
		modelTableMappedDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public ModelTableMapped get(Long id) {
		return modelTableMappedDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @return
	 */
	@Override
	public List<ModelTableMapped> getAll() {
		return modelTableMappedDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:53:23
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:58:25
	 * @modified by
	 * @param id
	 */
	@Override
	public void deleteByModelTableId(Long id) {
		modelTableMappedDao.excuteSql("DELETE FROM CODER_MODEL_TABLE_MAPPED WHERE TABLE_ID=?", id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午7:58:25
	 * @modified by
	 * @param modelTable
	 * @param mappedItems
	 */
	@Override
	public void saveModelMapByTable(ModelTable modelTable,
			List<ModelTableMappedItem> mappedItems) {
		ModelTableMapped modelTableMapped = null;
		ModelTable tempModelTable = null;
		for (ModelTableMappedItem modelTableMappedItem : mappedItems) {
			modelTableMapped = new ModelTableMapped();
			modelTableMapped.setMappedBy(modelTableMappedItem.getMappedBy());
			modelTableMapped.setMappedType(modelTableMappedItem.getMappedType());
			//设置java实体名称
			tempModelTable = modelTableService.getModelTableByTableName(modelTableMappedItem.getMappedBy());
			if (tempModelTable!=null) {
				modelTableMapped.setMappedByModel(tempModelTable.getModelName());
			}
			modelTableMapped.setModelTable(modelTable);
			modelTableMappedDao.save(modelTableMapped);
		}
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-3-6下午8:28:08
	 * @modified by
	 * @param tableId
	 * @return
	 */
	@Override
	public List<ModelTableMappedItem> loadModelMapByTableId(Long tableId) {
		List<ModelTableMapped> modelTableMappeds = modelTableMappedDao.findByHql("from ModelTableMapped where modelTable.id=?", tableId);
		return ModelTableMappedItem.createItems(modelTableMappeds);
	}

}
